/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#include <string.h>
#include "bitOps.h"

#ifndef NULL
#define NULL (void *)0
#endif


//*****************************************************************************
//*****************************************************************************

char *printBits(char *bits, uint32_t v, int num)
{
  int c;
  if (!bits || num < 0 || num > 32) return NULL;
  for (c=0; c < num; c++) {
    bits[num-(c+1)] = (v & 0x01) + 0x30; // Convert to number.
    v >>= 1;
  }
  bits[num] = '\0'; // Insert terminating NULL
  return bits;
}
//*****************************************************************************

char *printBits64(char *bits, uint64_t v, int num)
{
  int c;
  if (!bits || num < 0 || num > 64) return NULL;
  for (c=0; c < num; c++) {
    bits[num-(c+1)] = (v & 0x01) + 0x30; // Convert to number.
    v >>= 1;
  }
  bits[num] = '\0'; // Insert terminating NULL
  return bits;
}
//*****************************************************************************

int readBits(char *str) 
{
  int  v = 0x00, c;
  int max;

  if (!str) return 0;
  max = strlen(str);
  if (max > 32) return 0;
  
  for (c=0x00; c < max; c++) {
    v <<= 1;
    v |= (str[c] == '1') ? 0x01 : 0x00;
  } 
  return v;
}
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************
//*****************************************************************************

